#!/bin/bash
#功能：
#    基于centos 6的vagex挂机一键包
#版本历史：
#    2013/05/18 
#作者：
#    Ksda
#备注：
#    尽量在screen下运行以防中途断线

#PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#export PATH

#flash_addr="http://vagex-debian.googlecode.com/files/libflashplayer.so"
gomez_addr="http://lastmile.gomez.com/PEERInstall.exe"
vagex_addr="http://vagex.com/vagex_add_on-1.6.1.xpi"
yvqm_addr="http://vagex-debian.googlecode.com/files/youtube_video_quality_manager-1.2-fx.xpi"
ssocks_addr="http://superb-sea2.dl.sourceforge.net/project/ssocks/ssocks-0.0.13.tar.gz"
ssocksd_addr="https://centosvps-autoshell.googlecode.com/svn/trunk/ssocksd"
xstartup_addr="https://centosvps-autoshell.googlecode.com/svn/trunk/xstartup"
xstartup2_addr="https://centosvps-autoshell.googlecode.com/svn/trunk/xstartup2"
flash_addr="https://centosvps-autoshell.googlecode.com/svn/trunk/flash-plugin-11.2.202.285-release.i386.rpm"
taskkill_addr="https://centosvps-autoshell.googlecode.com/svn/trunk/taskkill.c"

func_check() {
    if [ $? != 0 ]; then
        echo "-----------"
        echo "ERROR! -> $1"
        echo "-----------"
        exit 1
    else
        echo "-----------"
        echo "DONE! -> $1"
        echo "-----------"
    fi    
}

func_in_ssocks() {
    yum -y install libX11-devel freetype-devel bison gcc make &&
    wget $ssocks_addr&&
    tar zxvf ssocks-0.0.13.tar.gz &&
    cd ssocks-0.0.13 &&
    ./configure &&
    make &&
    make install && 
    
cat >/usr/local/etc/ssocksd.auth <<EOF
KsdaAtBWH:10dPyToT
EOF

cat >/usr/local/etc/ssocksd.conf <<EOF
port = 31080
auth = /usr/local/etc/ssocksd.auth
daemon = 0
EOF

    wget $ssocksd_addr&&
    cp ssocksd /etc/init.d/
    chmod +x /etc/init.d/ssocksd &&
    chkconfig ssocksd on &&
    /etc/init.d/ssocksd restart 
    
    func_check ssocks-installation    
}


func_in_userpwd(){
    echo "-----------"
    echo "Plese input the Users password below"
    echo "-----------"
}


func_in_vnc_ksdafang() {
	echo "-----------"
	echo "Plese input the VNC password below for ksdafang"
	echo "-----------"
	su ksdafang -c "vncserver :1 && vncserver -kill :1" &&
	wget $xstartup_addr &&
	mv /home/ksdafang/.vnc/xstartup /home/ksdafang/.vnc/xstartupbak &&
	mv xstartup /home/ksdafang/.vnc && 
	chmod 755 /home/ksdafang/.vnc/xstartup
}

func_in_vnc_atrong() {
	echo "-----------"
	echo "Plese input the VNC password below for atrong"
	echo "-----------"
	su atrong -c "vncserver :2 && vncserver -kill :2"
	wget $xstartup2_addr &&
	mv /home/atrong/.vnc/xstartup /home/atrong/.vnc/xstartupbak &&
	mv xstartup2 /home/atrong/.vnc/xstartup && 
	chmod 755 /home/atrong/.vnc/xstartup
}

func_in_user(){
	groupadd atrong &&
	useradd -g atrong -d /home/atrong -s /bin/bash -m atrong &&
	useradd -g atrong -d /home/ksdafang -s /bin/bash -m ksdafang    
	func_in_userpwd
	passwd ksdafang
	passwd atrong
}

func_in_vnc() {
	yum -y install tightvnc tightvnc-server tightvnc-server-module fontforge &&
  func_in_user
	func_in_vnc_ksdafang	
	func_in_vnc_atrong
   
 chkconfig vncserver on 
	
	echo 'VNCSERVERS="1:ksdafang 2:atrong"' >> /etc/sysconfig/vncservers
	echo 'VNCSERVERARGS[1]="-geometry 800x600"' >> /etc/sysconfig/vncservers
	echo 'VNCSERVERARGS[2]="-geometry 800x600"' >> /etc/sysconfig/vncservers
	
	echo "* 3 * * * rm -rf /home/ksdafang/.vnc/*.log /tmp/plugtmp* > /dev/null 2>&1" >> /etc/crontab
	echo "* 3 * * * rm -rf /home/atrong/.vnc/*.log /tmp/plugtmp* > /dev/null 2>&1" >> /etc/crontab
	echo "* 5 * * * root /etc/init.d/vncserver restart" >> /etc/crontab
	echo "* 13 * * * root /etc/init.d/vncserver restart" >> /etc/crontab
	echo "* 21 * * * root /etc/init.d/vncserver restart" >> /etc/crontab
   echo "* */1 * * * root sh /root/autoresolv.sh" >> /etc/crontab
   
		
	yum -y groupinstall xfce
 }

func_in_ff(){
 yum install -y firefox &&
	dbus-uuidgen --ensure &&	
	wget $flash_addr&&
	rpm -ivh flash-plugin-*.rpm &&
	

mkdir /etc/adobe &&
bash -c "echo 'OverrideGPUValidation = 0' >> /etc/adobe/mms.cfg" &&
bash -c "echo 'ProtectedMode = 0' >> /etc/adobe/mms.cfg"

mkdir /usr/lib/firefox/distribution/bundles/disablesafemode/content -p

cat >/usr/lib/firefox/distribution/bundles/disablesafemode/chrome.manifest <<EOF
content disablesafemode content/
override chrome://browser/content/safeMode.xul chrome://disablesafemode/content/safeMode.xul
EOF

cat >/usr/lib/firefox/distribution/bundles/disablesafemode/content/safeMode.xul <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://global/skin/"?>
<dialog xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        id="safeModeDialog"
        buttons=","
        onload="closeFirefox();">
  <script type="application/x-javascript;version=1.7">
  <![CDATA[
  function closeFirefox() {
    Components.utils.import("resource://gre/modules/Services.jsm");
    Services.startup.quit(Services.startup.eForceQuit);
  }
  ]]>
  </script>
  <description>
  Safe Mode has been disabled.
  </description>
</dialog>
EOF

	/etc/init.d/vncserver start
	
	func_check vnc-installation	
}

func_in_repo(){
# rpmforge
 rpm --import http://apt.sw.be/RPM-GPG-KEY.dag.txt  &&
 rpm -Uvh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm &&
 
# EPEL
rpm --import http://archive.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6 &&
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm  &&

# remi
rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi  &&
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
}
 

func_in_repo

yum remove portmap* httpd* bind9* samba* nscd* sendmail* -y &&

yum update -y &&



func_in_vnc

echo "date >> /root/rtime.txt" >> /etc/rc.d/rc.local
echo "sh /root/autoresolv.sh" >> /etc/rc.d/rc.local

echo 'cat > /etc/resolv.conf <<EOF
nameserver 208.67.222.222
nameserver 208.67.220.220
nameserver 8.8.8.8
nameserver 8.8.4.4
EOF' >> /root/autoresolv.sh

func_in_ff

func_in_ssocks

su ksdafang -c "cd ~ && wget $vagex_addr" 
su ksdafang -c "cd ~ && wget $yvqm_addr" 
su atrong -c "cd ~ && wget $gomez_addr" 





func_in_wine(){


wget http://winetricks.org/winetricks &&
cp winetricks /usr/bin &&
chmod +x /usr/bin/winetricks &&


yum -y install libX11-devel freetype-devel bison gcc make &&
#rpm -qa | grep flex- |xargs rpm -e 

cd ~ &&
wget http://superb-dca3.dl.sourceforge.net/project/flex/flex-2.5.37.tar.bz2 &&
tar -xvf flex-2.*.tar.bz2 &&
cd flex-2.* &&
./configure &&
make && 
make install &&
su atrong -c "
cd ~ &&
wget http://downloads.sourceforge.net/project/wine/Source/wine-1.5.31.tar.bz2 &&
tar -xvf wine-1.5.*.tar.bz2 &&
cd wine-1.5.*/ &&
wget $taskkill_addr &&
mv ./programs/taskkill/taskkill.c ./programs/taskkill/taskkill.bak &&
mv taskkill.c ./programs/taskkill/taskkill.c"
}

func_in_wine